﻿// Copyright (C) Jari Hautio <jari.hautio@iki.fi> 2009. Licensed under GPLv2. See LICENSE.txt in solution folder.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace RyijyApp
{

    class ColorItemBinder : BindingList<CustomPalette.ColorItem>
    {
        private CustomPalette palette;
        public CustomPalette Palette
        {
            get { return palette; }
            set
            {
                palette = value;
                Reload();
                OnListChanged(resetEventArgs);
            }
        }
        private CustomPalette excludedPalette = null;
        public CustomPalette ExcludePalette
        {
            get { return excludedPalette; }
            set
            {
                excludedPalette = value;
                Reload();
                OnListChanged(resetEventArgs);
            }
        }
        private ListChangedEventArgs resetEventArgs = new ListChangedEventArgs(ListChangedType.Reset, 0);
        PropertyDescriptor sortProp = null;
        ListSortDirection sortDir;
        public ColorItemBinder(CustomPalette customPalette)
        {
            palette = customPalette;
            Reload();
        }
        public void RefreshColors()
        {
            ApplySortCore(sortProp, sortDir);
        }
        void Reload()
        {
            ApplySortCore(null, ListSortDirection.Ascending);
        }
        protected override void RemoveItem(int index)
        {
            palette.RemoveColor(Items[index].Color);
            base.RemoveItem(index);
        }
        protected override void OnAddingNew(AddingNewEventArgs e)
        {
            base.OnAddingNew(e);
        }
        protected override void InsertItem(int index, CustomPalette.ColorItem item)
        {
            if (!palette.Contains(item.Color))
            {
                palette.Colors.Add(item);
            }
            base.InsertItem(index, item);
        }
        protected override ListSortDirection SortDirectionCore
        {
            get
            {
                return sortDir;
            }
        }
        protected override PropertyDescriptor SortPropertyCore
        {
            get
            {
                return sortProp;
            }
        }
        protected override bool IsSortedCore
        {
            get
            {
                return sortProp != null;
            }
        }
        private IComparer<CustomPalette.ColorItem> GetComparer(PropertyDescriptor prop, ListSortDirection direction)
        {
            IComparer<CustomPalette.ColorItem> comp = null;
            if (prop == null) return null;

            if (prop.Name == "Name")
                comp = new CustomPalette.ItemComparer(
                    CustomPalette.ItemComparer.Mode.Name,
                    direction == ListSortDirection.Descending);
            else if (prop.Name == "Text")
                comp = new CustomPalette.ItemComparer(
                    CustomPalette.ItemComparer.Mode.Text,
                    direction == ListSortDirection.Descending);
            else if (prop.Name == "Color")
                comp = new CustomPalette.ItemComparer(
                    CustomPalette.ItemComparer.Mode.Color,
                    direction == ListSortDirection.Descending);
            return comp;
        }
        protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
        {
            List<CustomPalette.ColorItem> l = new List<CustomPalette.ColorItem>();
            l.AddRange(palette.Colors);
            IComparer<CustomPalette.ColorItem> comp = GetComparer(prop, direction);
            if (comp != null)
            {
                l.Sort(comp);
            };

            sortProp = prop;
            sortDir = direction;

            Items.Clear();
            foreach (CustomPalette.ColorItem c in l)
            {
                if (excludedPalette != null && excludedPalette.Contains(c.Color))
                    continue;
                Add(c);
            }
            OnListChanged(resetEventArgs);
        }
        protected override void RemoveSortCore()
        {
            sortProp = null;
            Reload();
            OnListChanged(resetEventArgs);
        }
        protected override bool SupportsSortingCore
        {
            get
            {
                return true;
            }
        }

    }
}